/* set more off */
/* capture log close */
/* clear */

use "./generated_data/3-budget_data_with_estimated_positions.dta"

/* CASE SELECTION */
/* -------------- */    
/* only keep ministers who participated in the budget debate (i.e.,
   drop those for which estimated policy positions are not available) */
drop if ws_cab_rescaled == .

/* drop reference documents */
/* keep if pm==0 & fm==0 & portfolio!="Taoiseach" & portfolio!="Finance"*/
drop if portfolio=="Taoiseach" | portfolio=="Finance"

/* VARIABLES */
/* --------- */    
/* encode portfolio varibale */
rename portfolio portfolio_tmp
encode portfolio_tmp, gen(portfolio)
drop portfolio_tmp

/* generate categorical variable for FMs */
replace fm = .
replace fm = 1 if budget_year>=1999 & budget_year<=2004
replace fm = 2 if budget_year>=2005 & budget_year<=2008
replace fm = 3 if budget_year>=2009 & budget_year<=2011
replace fm = 4 if budget_year>=2012 & budget_year<=2013

label def fm 1 "McCreevy" 2 "Cowen" 3 "Lenihan" 4 "Noonan"
label val fm fm


/* generate categorical variable for PMs */
replace pm = .    
replace pm = 1 if budget_year>=1999 & budget_year<=2008
replace pm = 2 if budget_year>=2009 & budget_year<=2011
replace pm = 3 if budget_year>=2012 & budget_year<=2013

label def pm 1 "Ahern" 2 "Cowen" 3 "Kenny"
label val pm pm


/* dummy variable for junior ministers */
gen junior = position=="Minister of State"

/* dummy variable: 1 if (junior)minister is from the same party as the PM */
gen same_party = (party_name=="Fianna Fáil" & budget_year>=1999 & budget_year<=2011) | (party_name=="Fine Gael" & budget_year>=2012 & budget_year<=2013)

/* add debt data to debate year */
preserve    
clear    
insheet using "./original_data/debt_data_worldbank.csv"
sort year
rename year debate_year
save "./generated_data/7-debt", replace
restore

sort debate_year
merge m:1 debate_year using "./generated_data/7-debt"
drop _merge
drop if debate_year < 1998 | debate_year > 2012


/* add unemployment data to debate year */
preserve
clear
insheet using "./original_data/annual_unemployment_rate.csv"
sort year
rename year debate_year
save "./generated_data/7-annual_unemployment_rate", replace
restore

sort debate_year
merge m:1 debate_year using "./generated_data/7-annual_unemployment_rate"
drop _merge
drop if debate_year < 1998 | debate_year > 2012


/* GEN CHANGES IN ECONOMIC MEASURES */
bysort debate_year: gen tmp = _n
replace tmp = . if tmp > 1
sort tmp debate_year


gen dpgChange_tmp = dpg - dpg[_n-1]
gen urChange_tmp  = ur - ur[_n-1]

replace dpgChange_tmp = . if tmp==. 
replace urChange_tmp  = . if tmp==. 

bysort debate_year: egen dpgChange = max(dpgChange_tmp)
bysort debate_year: egen urChange  = max(urChange_tmp)

drop tmp *_tmp

corr dpg ur
corr dpgChange urChange


/* SAVE DATA THAT'S BEING USED FOR REGRESSION MODELS */
save "./generated_data/7-data_for_regression_models.dta", replace


/* REGRESSION OUTPUT IN LATEX -- ONE LARGE TABLE */
label var junior "Junior minister"
label var same_party "Same party"
label var dpgChange "Change in debt"
label var urChange "Change in unemployment rate"
label var ws_cab_rescaled "Intra-cabinet position"

/* conditional on debt */
estimates clear
reg change_capital_share junior same_party c.dpgChange##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store debt_capital

reg change_current_share junior same_party c.dpgChange##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store debt_current


/* conditional on unemployment */    
reg change_capital_share junior same_party c.urChange##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store ur_capital

reg change_current_share junior same_party c.urChange##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store ur_current


/* conditional on PM */    
reg change_capital_share junior same_party i.pm##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store pm_capital

reg change_current_share junior same_party i.pm##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store pm_current

/* #delimit; */
/* estout * using "./tables/table1-regression_output_all_models.tex", style(tex) */
/* cells("b(star fmt(2))" "se(par fmt(2))") */
/* stats(r2 N, labels(R-squared "N. of cases") fmt(2 0)) */
/* starlevels(* 0.05) */
/* label legend */
/* varlabels(_cons Intercept) */
/* replace; */
/* #delimit cr */

/* PRINT TABLE 1 IN PAPER */
#delimit;
estout *,
cells("b(star fmt(2))" "se(par fmt(2))")
stats(r2 N, labels(R-squared "N. of cases") fmt(2 0))
starlevels(* 0.05)
label legend
varlabels(_cons Intercept)
replace;
#delimit cr


/* REGRESSION MODEL CONDITIONAL ON CHANGES IN DEBT */
gen change = dpgChange
    
reg change_capital_share junior same_party c.change##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)


margins, dydx(change) at(ws_cab_rescaled=(-1(.1)1)) vsquish
margins, at(ws_cab_rescaled=(-1(.1)1) change=(-12 -6 0 6 12 18 24) ) vsquish

#delimit ;
marginsplot,
  by(change)
  xlabel(-1(0.5)1)
  ylabel(-0.5(0.25)0.5)
  xtitle("Intra-cabinet position", size(6))
  ytitle("Linear prediction", size(6))
  byopts(rows(1) title("Marginal effect of changes in debt conditional on intra-cabinet positions", size(6)) iscale(*1.5))
  recast(line)
  recastci(rline)
  ciopts(lpattern(dash))
  yline(0)
  scheme(s1mono)
  xsize(10);
#delimit cr

graph export "./plots/figure3-predicted_effects_conditional_on_changes_in_debt.pdf", replace
   
    
/* REGRESSION MODEL CONDITIONAL ON CHANGES IN UNEMPLOYMENT RATE */
replace change = urChange
    
reg change_capital_share junior same_party c.change##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)

margins, dydx(change) at(ws_cab_rescaled=(-1(.1)1)) vsquish
margins, at(ws_cab_rescaled=(-1(.1)1) change=(-2 0 2 4 6) ) vsquish

#delimit ;
marginsplot,
  by(change)
  xlabel(-1(0.5)1)
  ylabel(-2(0.5)0.5)
  xtitle("Intra-cabinet position", size(6))
  ytitle("Linear prediction", size(6))
  byopts(rows(1) title("Marginal effect of changes in unemployment rate conditional on intra-cabinet positions", size(6)) iscale(*1.5))
  recast(line)
  recastci(rline)
  ciopts(lpattern(dash))
  yline(0)
  scheme(s1mono)
  xsize(10);
#delimit cr

graph export "./plots/figure3-predicted_effects_conditional_on_changes_in_unemployment.pdf", replace
    

/* REGRESSION MODEL CONDITIONAL ON PM IN OFFICE */
reg change_capital_share junior same_party i.pm##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)

margins, dydx(pm) at(ws_cab_rescaled=(-1(.1)1))
margins, at(ws_cab_rescaled=(-1(.1)1) pm=(1 2 3))

#delimit ;
marginsplot,
  by(pm)
  xlabel(-1(0.5)1)
  ylabel(-0.5(0.25)0.5)
  xtitle("Intra-cabinet position", size(6))
  ytitle("Linear prediction", size(6))
  byopts(rows(1) title("Marginal effect of PM in office conditional on intra-cabinet positions", size(6)) iscale(*1.5))
  recast(line)
  recastci(rline)
  ciopts(lpattern(dash))
  yline(0)
  scheme(s1mono)
  xsize(10);
#delimit cr

graph export "./plots/figure3-predicted_effects_conditional_on_PM.pdf", replace

